<%@ page import="java.util.Date" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Properties" %>
<jsp:useBean id="Constants" class="battleship.Constants" scope="page" />
<jsp:useBean id="GameTimestamp" class="battleship.GameTimestamp" scope="page" />
<jsp:useBean id="LoginInfo" class="battleship.LoginInfo" scope="page" />
<jsp:useBean id="Invite" class="battleship.Invite" scope="page" />
<% if (!LoginInfo.loggedIn(session)) { %>
    <jsp:forward page="index.jsp" />
<% } %>
<%= Constants.DOCTYPE %>
<html>
<head>
<%= Constants.META_CONTENT %>
<title><%= Constants.TITLE %> Invites</title>
<%= Constants.LINK_CSS %>
<body>
<% Invite.processRequest(request); %>
<%= LoginInfo.getLinks(request) %>
<table class="bordered">
    <caption>Games in Progress</caption>
    <tr>
        <th>User ID</th>
        <th>Last move</th>
        <th>Turn</th>
    </tr>
    <% List<String[]> list = Invite.getCurrentGameInfo(session);
    final Map<String,String> nicknames = Invite.getNicknames();
    final String pid = session.getAttribute(Constants.ATTR_UID).toString();
    for (final String[] row : list) {
        final String gid = row[0];
        String opponent = row[1];
        String turn = row[2]; 
        if (nicknames.containsKey(opponent)) {
        	opponent = nicknames.get(opponent);
        }
        if (nicknames.containsKey(turn)) {
        	turn = nicknames.get(turn);
        }
        %>
        <tr>
            <td><a href="game.jsp?gid=<%= gid %>"><%= opponent %></a></td>
            <td><%= new Date(GameTimestamp.getTimestamp(gid)) %></td>
            <td><%= String.format("<span class=\"%s\">%s</span>", pid.equals(turn) ? "yourmove" : "", turn) %></td>
        </tr>
    <% } %>
</table>

<table class="bordered">
    <caption>Completed Games</caption>
    <tr>
        <th>User ID</th>
        <th>Date completed</th>
    </tr>
    <% final Properties p = Invite.getFinishedGameInfo(session);
    for (final Object gidO : p.keySet()) {
        final String gid = gidO.toString(); %>
        <tr>
            <td><a href="game.jsp?gid=<%= gid %>"><%= p.getProperty(gid) %></a></td>
            <td><%= new Date(GameTimestamp.getTimestamp(gid)) %></td>
        </tr>
    <% } %>
</table>

<p><form action="manage.jsp" method="post">
<input type="hidden" name="invitee" value="<%= Constants.COMPUTER_UID %>">
<input type="hidden" name="<%= Constants.COMPUTER_UID %>" value="<%= Invite.NAME_ACCEPT %>">
<input type="submit" value="Start New Single-Player Game">
</form></p>

<p><form action="manage.jsp" method="post">User ID: <input type="text" name="invitee">
<input type="submit" value="Send invite">
</form></p>

<form action="manage.jsp" method="post">
<table class="bordered">
    <caption>Invitations Sent to You</caption>
    <tr>
        <th>User ID</th>
        <th>Accept?</th>
        <th>Reject?</th>
    </tr>
    <% for (final String uid : Invite.getInviters(session)) { %>
        <tr>
            <td><%= uid %></td>
            <td style="text-align: center;"><input type="radio" name="<%= uid %>" value="<%= Invite.NAME_ACCEPT %>"></td>
            <td style="text-align: center;"><input type="radio" name="<%= uid %>" value="<%= Invite.NAME_REJECT %>"></td>
        </tr>
    <% } %>
    <tr>
        <td colspan="3" style="text-align: center;">
        <input type="submit" value="Save" style="width: 100%;">
        </td>
    </tr>
</table>
</form>

<form action="manage.jsp" method="post">
<table class="bordered">
    <caption>Invitations Sent by You</caption>
    <tr>
        <th>User ID</th>
        <th>Cancel?</th>
    </tr>
    <% for (final String uid : Invite.getInvitees(session)) { %>
        <tr>
            <td><%= uid %></td>
            <td style="text-align: center;"><input type="checkbox" name="<%= Invite.NAME_CANCEL %>" value="<%= uid %>"></td>
        </tr>
    <% } %>
    <tr>
        <td colspan="2" style="text-align: center;">
        <input type="submit" value="Cancel" style="width: 100%;">
        </td>
    </tr>
</table>
</form>
</div>
</body>
</html>